package com.ra.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ra.exception.BizException;
import com.ra.pojo.Photo;
import com.ra.mapper.PhotoMapper;
import com.ra.pojo.PhotoAlbum;
import com.ra.pojo.RespPageBean;
import com.ra.pojo.dto.*;
import com.ra.service.IPhotoAlbumService;
import com.ra.service.IPhotoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ra.utils.BeanCopyUtil;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import static com.ra.constant.CommonConstant.FALSE;
import static com.ra.constant.CommonConstant.ONE;

/**
 * <p>
 * 照片 服务实现类
 * </p>
 *
 * @author 今晚几点睡_Ada
 * @since 2023-03-09
 */
@Service
public class PhotoServiceImpl extends ServiceImpl<PhotoMapper, Photo> implements IPhotoService {

    @Autowired
    private PhotoMapper photoMapper;
    @Autowired
    private IPhotoAlbumService photoAlbumService;
    @Override
    public RespPageBean<PhotoAdminDTO> listPhotos(ConditionDTO conditionDTO) {
        //分页查询
        Page<Photo> photoPage = photoMapper.selectPage(new Page<>(conditionDTO.getCurrent(), conditionDTO.getSize()),
                new LambdaQueryWrapper<Photo>()
                        //conditionDTO.getAlbumId()不为空 并且 Photo的相册id等于查询条件的相册id
                        .eq(Objects.nonNull(conditionDTO.getAlbumId()), Photo::getAlbumId, conditionDTO.getAlbumId())
                        //isdelete
                        .eq(Photo::getIsDelete, conditionDTO.getIsDelete())
                        //根据id和更新时间排序
                        .orderByDesc(Photo::getId)
                        .orderByDesc(Photo::getUpdateTime)
        );
        List<PhotoAdminDTO> photoAdminDTOS = BeanCopyUtil.copyList(photoPage.getRecords(), PhotoAdminDTO.class);

        Integer size = photoPage.getRecords().size();
        return new RespPageBean<>(size.longValue(),photoAdminDTOS);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updatePhoto(PhotoInfoDTO photoInfoDTO) {
        Photo photo = BeanCopyUtil.copyObject(photoInfoDTO, Photo.class);
        photoMapper.updateById(photo);
    }
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void savePhotos(PhotoDTO photoDTO) {
        List<Photo> photoList = photoDTO.getPhotoUrls().stream().map(item -> Photo.builder()
                .albumId(photoDTO.getAlbumId())
                .photoName(IdWorker.getIdStr())
                .photoSrc(item)
                .build())
                .collect(Collectors.toList());
        this.saveBatch(photoList);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updatePhotosAlbum(PhotoDTO photoDTO) {
        List<Photo> photoList = photoDTO.getPhotoIds().stream().map(item -> Photo.builder()
                .id(item)
                .albumId(photoDTO.getAlbumId())
                .build())
                .collect(Collectors.toList());
        this.updateBatchById(photoList);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updatePhotoDelete(DeleteDTO deleteDTO) {
        List<Photo> photoList = deleteDTO.getIds().stream().map(item -> Photo.builder()
                .id(item)
                .isDelete(deleteDTO.getIsDelete())
                .build())
                .collect(Collectors.toList());
        this.updateBatchById(photoList);
        if (deleteDTO.getIsDelete().equals(FALSE)) {
            List<PhotoAlbum> photoAlbumList = photoMapper.selectList(new LambdaQueryWrapper<Photo>()
                    .select(Photo::getAlbumId)
                    .in(Photo::getId, deleteDTO.getIds())
                    .groupBy(Photo::getAlbumId))
                    .stream()
                    .map(item -> PhotoAlbum.builder()
                            .id(item.getAlbumId())
                            .isDelete(FALSE)
                            .build())
                    .collect(Collectors.toList());
            photoAlbumService.updateBatchById(photoAlbumList);
        }
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void deletePhotos(List<Integer> photoIds) {
        photoMapper.deleteBatchIds(photoIds);
    }

    @SneakyThrows
    @Override
    public SeePhoto listPhotosByAlbumId(Integer albumId) {
        PhotoAlbum photoAlbum = photoAlbumService.getOne(new LambdaQueryWrapper<PhotoAlbum>()
                .eq(PhotoAlbum::getId, albumId)
                .eq(PhotoAlbum::getIsDelete, FALSE)
                .eq(PhotoAlbum::getStatus, ONE));
        if (Objects.isNull(photoAlbum)) {
            throw new BizException("相册不存在");
        }
        Page<Photo> page = new Page<>(0, 10);
        List<String> photos = photoMapper.selectPage(page, new LambdaQueryWrapper<Photo>()
                .select(Photo::getPhotoSrc)
                .eq(Photo::getAlbumId, albumId)
                .eq(Photo::getIsDelete, FALSE)
                .orderByDesc(Photo::getId))
                .getRecords()
                .stream()
                .map(Photo::getPhotoSrc)
                .collect(Collectors.toList());


        return SeePhoto.builder()
                .photoAlbumCover(photoAlbum.getAlbumCover())
                .photoAlbumName(photoAlbum.getAlbumName())
                .photos(photos)
                .build();
    }





}
